package com.lwldjx.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.lwldjx.auth.application.convert.AuthPermissionDTOConverter;
import com.lwldjx.auth.application.convert.AuthRoleDTOConverter;
import com.lwldjx.auth.application.convert.AuthUserDTOConverter;
import com.lwldjx.auth.application.dto.AuthPermissionDTO;
import com.lwldjx.auth.application.dto.AuthRoleDTO;
import com.lwldjx.auth.application.dto.AuthUserDTO;
import com.lwldjx.auth.common.entity.Result;
import com.lwldjx.auth.domain.entity.AuthPermissionBO;
import com.lwldjx.auth.domain.entity.AuthRoleBO;
import com.lwldjx.auth.domain.entity.AuthUserBO;
import com.lwldjx.auth.domain.service.AuthPermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/permission/")
@Slf4j
public class PermissionController {
    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    @RequestMapping("add")
    public Result<Boolean> register(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try{
            if (log.isInfoEnabled()) {
                log.info(" PermissionController.add.dto:{}",  JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()), "权限名称不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(), "权限父级id不能为空");
            AuthPermissionBO authPermissionBo= AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            Boolean result =authPermissionDomainService.add(authPermissionBo);
            return Result.ok(result);
        }catch (Exception e){
            log.error("AuthPermissionController.add.error:{}",e.getMessage(),e);
            return Result.fail("增加权限失败"+e.getMessage());
        }
    }
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try{
            if (log.isInfoEnabled()) {
                log.info(" PermissionController.update.dto:{}",  JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空");
            AuthPermissionBO authPermissionBo= AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            Boolean result =authPermissionDomainService.update(authPermissionBo);
            return Result.ok(result);
        }catch (Exception e){
            log.error("AuthPermissionController.update.error:{}",e.getMessage(),e);
            return Result.fail("更新权限失败"+e.getMessage());
        }
    }
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try{
            if (log.isInfoEnabled()) {
                log.info(" PermissionController.delete.dto:{}",  JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(),"ID不能为空");
            AuthPermissionBO authPermissionBo= AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            Boolean result =authPermissionDomainService.delete(authPermissionBo);
            return Result.ok(result);
        }catch (Exception e){
            log.error("AuthRoleController.delete.error:{}",e.getMessage(),e);
            return Result.fail("删除权限失败"+e.getMessage());
        }
    }
    @RequestMapping("deleteById")
    public Result<Boolean> deleteById(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try{
            if (log.isInfoEnabled()) {
                log.info(" PermissionController.deleteById.dto:{}",  JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(),"ID不能为空");
            AuthPermissionBO authPermissionBo= AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            Boolean result =authPermissionDomainService.deleteById(authPermissionBo);
            return Result.ok(result);
        }catch (Exception e){
            log.error("AuthPermissionController.deleteById.error:{}",e.getMessage(),e);
            return Result.fail("删除权限失败"+e.getMessage());
        }

    }
    @RequestMapping("changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.changeStatus.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getStatus(), "权限状态不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.changeStatus(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.changeStatus.error:{}", e.getMessage(), e);
            return Result.fail("启用/禁用权限信息失败");
        }
    }
    @RequestMapping("changeShow")
    public Result<Boolean> changeShow(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.changeShow.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getShow(), "权限展示不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.changeShow(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.changeShow.error:{}", e.getMessage(), e);
            return Result.fail("启用/禁用权限信息失败");
        }
    }
}

